<template>
	<view class="logins">
		<nav-bar isBack backColor="#fff" title="手机登录" color="#fff" background="transparent" isPlace="false"></nav-bar>
		
		<!-- 顶部 -->
		<view class="phonetop">			
			<view class="l_top">				
				<image :src="`${baseImg}/static/ryzLogo/logo-2.png`"></image>		
			</view>
			<view class="conmt">
				<view class="call">
					<text class="left-text">手机号</text>
					<input type="number" placeholder="请输入手机号" v-model="mobile"/>
				</view>
				<view class="call">
					<text class="left-text">验证码</text>
					<input type="number" placeholder="请输入验证码" v-model="sms_code"/>
					<text class="right-text"  v-if="time===60"@click="getCodeEvent">获取验证码</text>
					<text class="right-text"  v-if="time!==60" style="color: #999;">{{time}}s</text>
				</view>
				<view class="loginbtn" @click="userBindMobile">登录</view>
			</view>
		</view>
	</view>
</template>

<script>
import navBar from "@/components/navBar/navBar.vue"
import service_enter from '@/service/enter.js'
import URL from '@/config/url.js'


export default {
	components: { navBar },
	
	data() {
		return {
			baseImg:this.$static,
			mobile:'',
			sms_code:'',
			time:60,
			timer:null,
			// 登录信息
			token: null,           // token
			isBindMobile: null,    // 是否绑定手机号
			userMobile: null,      // 用户手机号
			userId: null,          // 用户userId
			role:'',
			sid:null,
			page:[],//权限
			// rootStatus:true,//是否有权限进入管理页面（针对被移除的创业精英）
			
			miniType:'', //是否外部小程序（ 中灿/药箱 ）跳转过来提现的标识---OtherMiniCashOut
		}
	},
	
	onLoad(options) {
		let app = getApp();
		this.miniType = app.globalData.miniType;
		console.log(app,this.miniType,'中灿提现')
	},
	
	methods: {
		getCodeEvent(){
				let phonenum = /^1[23456789]\d{9}$/.test(this.mobile);
				if(!this.mobile){
					uni.showToast({ title: '请输入手机号', icon: 'none' });
					return
				}
				if(!phonenum){
					uni.showToast({ title: '请输入正确的手机号码', icon: 'none' });
					return
				}
				
				service_enter.getPhonecode({
					mobile:this.mobile
				}).then(res=>{
					if(res.code === 1){
						this.timeInterval();
						uni.showToast({ title: '已发送验证码！', icon: 'none' });
					}else{
						uni.showToast({ title: res.msg, icon: 'none' });
					}
					
				})
			},
			timeInterval(){
				this.time = this.time - 1;
				this.timer = setInterval(()=>{
					if(this.time-1<1){
						clearInterval(this.timer)
						this.timer = null;
						this.time = 60
					}else{
						this.time = this.time - 1;
					}
				},1000)
			},
			async userBindMobile(){
				let phonenum = /^1[23456789]\d{9}$/.test(this.mobile);
				let single_id = uni.getStorageSync('single_id')
				let payStatus = uni.getStorageSync('payStatus')=='true'?true:false
				let follow_id = uni.getStorageSync('follow_id')
				console.log(uni.getStorageInfoSync())
				let that = this
				if(!this.mobile){
					uni.showToast({ title: '请输入手机号', icon: 'none' });
					return
				}
				if(!phonenum){
					uni.showToast({ title: '请输入正确的手机号码', icon: 'none' });
					return
				}
				if(!this.sms_code){
					uni.showToast({ title: '请输入验证码', icon: 'none' });
					return
				}
				this.$utils.showLoading()
				await service_enter.getUserPhone({
					mobile:this.mobile,
					sms_code:this.sms_code
				}).then(async res=>{
					console.log(res)
					if(res.code === 1){
						if(res.data.is_has_headimg!=1 || res.data.is_has_nickname!=1){
							if(uni.getUserProfile){
								uni.getUserProfile({
									desc:'Wexin',  // 这个参数是必须的
									success:res=>{
										console.log(res)
										if(res.errMsg == 'getUserProfile:ok'){
											let params = {
												headimg:res.userInfo.avatarUrl,
												nickname:res.userInfo.nickName
											}
											service_enter.changeUserInfo(params).then(res=>{
												console.log(res,'更换用户信息')
												if(res.code==1){}
											})
										}
									},
									fail:err=>{
										console.log(err)
									}
								})
							}
						}
						this.$utils.hideLoading()
						let data = res.data
						let isBindMobile = data.bind_mobile_status == 1 ? true : false   // 用户是否绑定手机号
						let token = data.token.access_token
						let role = data.role
						let userId = data.user_id
						let sid = data.sid
						let userMobile = data.mobile
						let count = data.count
						let qyUserId = data.qy_user_id
						let identityType = data.identity_type
						let isMaster = data.is_master
						let marketId = data.market_id
						let moduleType = data.type; //超级管家类型
						let moduleTeamId = data.steward_id; //超级管家团队id
						// 保存数据
						this.token = token
						this.isBindMobile = isBindMobile
						this.userMobile = userMobile
						this.userId = userId
						this.role = role
						this.sid = sid
						this.$store.commit('index/SET_token', token)
						this.$store.commit('index/SET_role', role)
						this.$store.commit('index/SET_sid', sid)
						this.$store.commit('index/SET_tokenTime', Date.now())
						this.$store.commit('index/SET_userMobile', userMobile)
						this.$store.commit('index/SET_userId', userId)
						this.$store.commit('index/SET_qyUserId', qyUserId)
						this.$store.commit('index/SET_identityType', identityType)
						this.$store.commit('index/SET_isMaster', isMaster)
						this.$store.commit('index/SET_storeCount', count)
						this.$store.commit('index/SET_marketId', marketId)
						this.$store.commit('index/SET_moduleType', moduleType)
						this.$store.commit('index/SET_moduleTeamId', moduleTeamId)
						await this.getAllRoot()
						if(that.miniType == 'OtherMiniCashOut'){ //其他小程序跳过来提现
							console.log(that.miniType)
							uni.reLaunch({
								url:'/pages_ZC_Wallet/pages/index'
							})
						}else if(follow_id){ //申请成为创客精英
							uni.reLaunch({
								url:`/pagesOne/distri/bandEntre/bindEntre?follow_id=${follow_id}`
							})
						}else if(single_id){ //修改优惠未登录时登陆完跳转
							if(payStatus){
								console.log(payStatus)
								console.log(payStatus===true)
								uni.reLaunch({
									url:`/pagesGuide/shopGuide/userPay/userPay?single_id=${single_id}`
								})
							}else{
								uni.reLaunch({
									url:`/pagesGuide/shopGuide/guideList/guideList?single_id=${single_id}`
								})
							}
						}else{
							if(this.role==''){
								// uni.reLaunch({
								// 	url: '/pages/enter/index/index',
								// });
								service_enter.getEnterStatus(userMobile).then(res=>{
									if(res.code==1){
										if(res.data.type==0){ //没有入驻
											uni.reLaunch({
												url: '/pages/enter/index/index',
											});
										}else if(res.data.type==1){
											// 申请中
											if (res.data.status === 0) {
												// this.enterGet = 'null'   // 下次重新获取审核状态
												uni.reLaunch({ url: "/pages/enter/index/audit" })
											}
											// 申请通过
											else if (res.data.status === 1) {
												uni.reLaunch({ url: "/pages/enter/index/success"})
											}
											// 申请失败
											else if (res.data.status === 2) {
												uni.reLaunch({ url: "/pages/enter/index/fail?reason=" + res.data.reason })
											}
										}else if(res.data.type==2){
											// 申请中
											if (res.data.status === 0) {
												// this.enterGet = 'null'   // 下次重新获取审核状态
												uni.reLaunch({ url: "/pagesOne/distri/bandEntre/success" })
											}
											// 申请通过
											else if (res.data.status === 1) {
												uni.reLaunch({ url: "/pagesOne/distri/index/index"})
											}
											// 申请失败
											else if (res.data.status === 2) {
												uni.reLaunch({ url: "/pagesOne/distri/bandEntre/fail?reason=" + res.data.reason })
											}
										}else if(res.data.type==3){//已入驻，跳转到商家首页
											uni.reLaunch({ url: "/pages/mall/newIndex/newIndex"})
											
										}else{//已成为创客，跳转到创客首页
											// if(!this.rootStatus){//被移除的创客精英（没有权限）
											// 	uni.switchTab({
											// 		url:'/pages/mall/index/index'
											// 	})
											// 	return
											// }
											uni.reLaunch({ url: "/pagesOne/distri/index/index"})
										}
									}
								})
								
							}else{
								if(count>1){
									uni.reLaunch({
										url:'/pages/enter/selectStore/index?mobile='+userMobile
									})
								}else{
									// 邀请超级管家的分享跳转
									let to_id = uni.getStorageSync('to_id'); //超级管家邀请团队成员时的id
									let steward_id = uni.getStorageSync('to_steward_id'); //超级管家邀请团队成员时的团队id
									if(to_id){
										uni.reLaunch({
											url:`/pages_superHK/pages/team/bindSuperHK?to_id=${to_id}&steward_id=${steward_id}`
										})
										return
									}
									// uni.switchTab({
									// 	url: '/pages/mall/index/index',
									// });
									
									let router = that.page?that.page.ability:[]
									console.log(router,that.page,'、、、、、')
									if(router.findIndex(item=>item.route == 'adminIndex')!=-1){ //总后台
										uni.reLaunch({
											url:'/pages_admin/home/home'
										})
									}else if(router.findIndex(item=>item.route == 'shopIndex')!=-1){ //商家
										uni.reLaunch({
											url:'/pagesFour/home/home'
										})
									}else if(router.findIndex(item=>item.route == 'designerIndex')!=-1){ //设计师
										uni.reLaunch({
											url:'/pageTwo/home/home'
										})
									}else if(router.findIndex(item=>item.route == 'builderIndex')!=-1){ //施工队
										uni.reLaunch({
											url:'/pagesOne/constr/home/home'
										})
									}else if(router.findIndex(item=>item.route == 'decorationIndex')!=-1){ //装修公司
										uni.reLaunch({
											url:'/pages_decoration/home/home'
										})
									}else if(router.findIndex(item=>item.route == 'promoterIndex')!=-1){
										// if(uni.getUserProfile){
										// 	uni.getUserProfile({
										// 		desc:'Wexin',  // 这个参数是必须的
										// 		success:res=>{
										// 			console.log(res)
										// 			if(res.errMsg == 'getUserProfile:ok'){
										// 				uni.setStorageSync('userInfo',res.userInfo)
														uni.reLaunch({
															url:'/pagesOne/distri/index/index'
														})
										// 			}
										// 		},
										// 		fail:err=>{
										// 			console.log(err)
										// 		}
										// 	})
										// }
									}else{
										uni.reLaunch({
											url:'/pages/enter/index/index'
										})
									}
								}
							}
						}
					}else{
						this.$utils.hideLoading()
						this.$utils.showErr({ title: res.msg })
					}
				})
			},
			// 所有权限
			async getAllRoot(){
				let params = {
					user_id:this.$store.state.index.userId,
					page:1,
					pagesize:10
				}
				await this.$api.allRoot(params).then(res=>{
					console.log(res,'所有权限')
					if(res.code==1){
						let page = res.data
						this.page = page
						this.$store.commit('index/SET_page', page)
					}
					// else if(res.code==-9){
					// 	this.rootStatus = false
					// 	// return false
					// }
				})
			},
		},
		onUnload() {
			clearInterval(this.timer)
			this.timer = null;
		}

	
}
</script>

<style lang="scss" scoped>
.logins{
	width: 100vw;height: 100vh;background: #f8f8f8;display: flex;flex-flow: column;
}
.phonetop{
	padding: 0 40rpx;
	padding-top:220rpx;
	background: url(https://manager.ryz1620.com/static/login/beijing.png) no-repeat top center;
	background-size: 100%;
}
.l_top{
	image{
		width: 250rpx;
		height: 250rpx;
		display: block;
		margin: auto;
		padding: 20rpx;
		background: #fff;
		border-radius: 50%;
	}
		background: url(@/static/login/bgt@2x.png) no-repeat bottom center;
		background-size: 100%;
}
.conmt{
		background: #fff;
		padding: 30rpx;
		padding-bottom: 80rpx;
		border-radius: 0 0 20rpx 20rpx;
		.call{
			position: relative;
			.left-text{
				display: block;
				font-size: 30rpx;
				font-weight: bold;
				line-height: 70rpx;
			}
			input{
				background: #F8F8F8;
				height: 80rpx;
				line-height: 80rpx;
				padding: 0 20rpx;
				margin-bottom: 50rpx;
			}
			.right-text{
				display:inline-block;
				text-align: center;
				position: absolute;
				right: 20rpx;
				bottom: 0;
				line-height: 80rpx;
				color: #DBA954;
				z-index: 99;
			}
		}
		.loginbtn{
			line-height: 88rpx;
			border-radius: 44rpx;
			text-align: center;
			background: #3D444B;
			font-size: 32rpx;
			color: #fff;
		}
}
</style>